package law_of_demeter;

@SuppressWarnings("unused")
public class Foo {
	/**
	 * This example will result in two violations.
	 */
	public void example1(Bar b) {

		// ok, as b is a parameter of "example"

		C c = b.getC(); // ok
	}

	public void example2(Bar b) {

		C c = b.getC();

		c.doIt(); // violation, object not created locally

		// as we are using c, which we got from B.
		// We should ask b directly instead, e.g. "b.doItOnC();"
	}

	public void example3(Bar b) {

		// a constructor call, not a method call.

		D d = new D(); // object created locally

		// this method call is ok, because
		// we have create the new instance of D locally.
		d.doSomethingElse();
	}

	public void example4(Bar b) {

		// just expressed differently as a method chain
		// without temporary variables.

		b.getC().doIt(); // violation, method chain calls

	}

}
